/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package engine;

import TransportLayer.IServiceListener;
import SynchronizationLayer.ISynchronizationServer;
import TransportLayer.NotaryRequestPacket;
import TransportLayer.NotaryRequestType;
import Repository.IRepositoryProxy;
import sntp.ISNTPClient;

/**
 *
 * @author Tomasz Nowak
 */
public class NotaryEngine
{
	protected NotaryEngine( IRepositoryProxy repository, IServiceListener serviceListener, ISNTPClient sntpClient, ISynchronizationServer synchronizationServer )
	{
		this.repository = repository;
		this.serviceListener = serviceListener;
		this.sntpClient = sntpClient;
		this.synchronizationServer = synchronizationServer;

		notaryState = NotaryState.Offline;

		HandleRequestFactory.register(NotaryRequestType.Verification, new VerificationHandler(this.repository,
																																													this.serviceListener));
		HandleRequestFactory.register(NotaryRequestType.Revocation, new RevocationHandler(this.repository,
																																											this.serviceListener,
																																											this.synchronizationServer));
		HandleRequestFactory.register(NotaryRequestType.Registration, new RegistrationHandler(this.repository,
																																													this.serviceListener,
																																													this.synchronizationServer));
	}

	IRepositoryProxy repository;
	IServiceListener serviceListener;
	ISNTPClient sntpClient;
	ISynchronizationServer synchronizationServer;
	NotaryState notaryState;

	private static NotaryEngine instance;

	public static NotaryEngine Create( IRepositoryProxy _RepositoryProxy,
																		 IServiceListener _ServiceListener,
																		 ISNTPClient _SNTPClient,
																		 ISynchronizationServer _SynchronizationServer )
	{
		if ( instance == null )
		{
			instance = new NotaryEngine(_RepositoryProxy, _ServiceListener, _SNTPClient, _SynchronizationServer);
		}
		return instance;
	}

	public void start()
	{

	}

	private void handleRequest( NotaryRequestPacket notaryRequestPacket )
	{
		IRequestHandler handler = HandleRequestFactory.getHandler(
						notaryRequestPacket.getNotaryRequest().getNotaryRequestType());

		handler.handleRequest(notaryRequestPacket);
	}
}
